package com.pig4cloud.pig.admin.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.api.entity.BusOperateDetailEntity;
import com.pig4cloud.pig.admin.service.BusOperateDetailService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 操作明细表
 *
 * @author pig
 * @date 2025-04-11 23:26:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/busOperateDetail" )
@Tag(description = "busOperateDetail" , name = "操作明细表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BusOperateDetailController {

    private final BusOperateDetailService busOperateDetailService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param busOperateDetail 操作明细表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
	@Inner(value = false)
    public R getBusOperateDetailPage(@ParameterObject Page page, @ParameterObject BusOperateDetailEntity busOperateDetail) {
        LambdaQueryWrapper<BusOperateDetailEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.orderByDesc(BusOperateDetailEntity::getCreateTime);
        return R.ok(busOperateDetailService.page(page, wrapper));
    }


    /**
     * 通过id查询操作明细表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
	@Inner(value = false)
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(busOperateDetailService.getById(id));
    }

    /**
     * 新增操作明细表
     * @param busOperateDetail 操作明细表
     * @return R
     */
    @Operation(summary = "新增操作明细表" , description = "新增操作明细表" )
    @SysLog("新增操作明细表" )
    @PostMapping
	@Inner(value = false)
    public R save(@RequestBody BusOperateDetailEntity busOperateDetail) {
        return R.ok(busOperateDetailService.save(busOperateDetail));
    }

    /**
     * 修改操作明细表
     * @param busOperateDetail 操作明细表
     * @return R
     */
    @Operation(summary = "修改操作明细表" , description = "修改操作明细表" )
    @SysLog("修改操作明细表" )
    @PutMapping
	@Inner(value = false)
    public R updateById(@RequestBody BusOperateDetailEntity busOperateDetail) {
        return R.ok(busOperateDetailService.updateById(busOperateDetail));
    }

    /**
     * 通过id删除操作明细表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除操作明细表" , description = "通过id删除操作明细表" )
    @SysLog("通过id删除操作明细表" )
    @DeleteMapping
	@Inner(value = false)
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(busOperateDetailService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param busOperateDetail 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
	@Inner(value = false)
    public List<BusOperateDetailEntity> export(BusOperateDetailEntity busOperateDetail, Long[] ids) {
        return busOperateDetailService.list(Wrappers.lambdaQuery(busOperateDetail).in(ArrayUtil.isNotEmpty(ids), BusOperateDetailEntity::getId, ids));
    }
}